<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/demo.js"></script>

    <!-- 等价于 -->
    <!-- <script>
        var box = document.getElementsByClassName("box")[0]; // [][0] => undefind
        box.onclick = function () {
            alert(111111);
        }
    </script> -->

    <style>
        * {
            padding: 0;
            margin: 0;
        }

        html {
            width: 3000px;
            height: 5000px;
        }

        .box {
            width: 200px;
            height: 200px;
            background-color: #6ff;
            margin: 50px;
        }
    </style>
</head>

<body>
    <div class="box"></div>
</body>
<script>
    // window相关的事件
    // onload     等页面所有内容加载完毕之后触发
    // onresize   窗口大小改变时触发
    // onscroll   页面的滚动条滚动时触发


    resizeHandler(); //页面加载时执行一次
    window.onresize = resizeHandler; // 事件触发时再次执行

    scrolllHandler();
    window.onscroll = scrolllHandler;

    function resizeHandler() {
        console.log("窗口大小改变", window.innerWidth, window.innerHeight);

        if (window.innerWidth >= 1024) {
            document.body.style.backgroundColor = "pink"
        } else if (window.innerWidth >= 768) {
            document.body.style.backgroundColor = "orange"
        } else if (window.innerWidth >= 320) {
            document.body.style.backgroundColor = "#6ff"
        }
    }

    function scrolllHandler() {
        console.log("scrolling", "获取滚动条距离文档左边和顶部的位置");
    }




</script>

</html>